﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GoogleMaps.LocationServices;

namespace Client
{
    public partial class Map : Form
    {
        public Map()
        {
            InitializeComponent();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void GoogleMap_Load(object sender, EventArgs e)
        {
            string lat = Lat.ToString("f4");
            string lon = Long.ToString("f4");
            if (Lat == 0 || Long == 0)
            {
                //45.4248, -75.6989
                lat = "45.4248";
                lon = "-75.6989";
            }
            
            try
            {
                StringBuilder queryAddress = new StringBuilder();
                queryAddress.Append("https://maps.google.com/maps?q=");

                if (lat != string.Empty)
                {
                    queryAddress.Append(lat + ",%20");
                }

                if (lon != string.Empty)
                {
                    queryAddress.Append(lon);
                }
                var s = queryAddress.ToString();
                webBrowser1.Navigate(queryAddress.ToString());

                var locationService = new GoogleLocationService();
                //var region = locationService.GetRegionFromLatLong(Lat, Long);
         
                //var point = locationService.GetLatLongFromAddress(queryAddress.ToString());
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error");
            }
        }

        public double Lat { get; set; }
        public double Long { get; set; }

        private void Map_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dynamic result = webBrowser1.Document.InvokeScript("eval",
    new object[] { "{x: 0, y: 0, url: document.URL}" });
            MessageBox.Show(result.url.ToString());
        }
    }
}
